﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SOM.AbstractDAL;
using SOM.AbstractDALFactory;
using SOM.ConcreteDALFactory;
using SOM.Model;

public partial class Usuario_AlterarSenhaUsuario : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSalvar_Click(object sender, EventArgs e)
    {
        try
        {
            if (IsValid)
            {
                Usuario usuario = SOM.Common.Common.RetornarUsuarioSessao(this);
                SOMAbstractDALFactory factory = new SOMConcreteDALFactory();
                AbstractUsuarioDAL usuarioDAL = factory.CreateUsuarioDAL();
                if (usuario.usr_senha.Equals(SOM.Security.SOMSecurity.MD5Criptografar(txtSenhaAntiga.Text)))
                {
                    usuario.usr_senha = SOM.Security.SOMSecurity.MD5Criptografar(txtNovaSenha.Text);
                    usuarioDAL.Update(usuario);

                    lblMensagem.Text = "Senha alterada com sucesso";
                    this.LimparFormulario();
                }
            }
        }
        catch(Exception ex)
        {
            lblMensagem.Text = ex.Message + " " + ex.InnerException;
        }

    }

    private void LimparFormulario()
    {
        Control masterPage = this.Controls[0];
        Control formulario = masterPage.FindControl("form1");
        Control content = formulario.FindControl("ContentCenter");

        foreach (Control control in content.Controls)
        {
            if (control is TextBox)
            {
                ((TextBox)control).Text = String.Empty;
            }
        }
    }
    protected void txtCancelar_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Usuario/CadastroUsuario.aspx?operacao=alteracao");
    }
}
